#include "../include/head.h"
// 初始化用户链表
u_list *init_user_list(void)
{
    u_list *user_list = malloc(sizeof(u_list));
    if (user_list == NULL)
    {
        ERRLOG("动态表创建失败\n");
        return NULL;
    }
    user_list->next = NULL;
    return user_list;
}
// 插入新节点到链表
void insert_node(u_list *user_list, user_t userdt, int pos)
{
    if (user_list == NULL)
    {
        ERRLOG("链表为空\n");
        return;
    }
    u_node *new_node = (u_node *)malloc(sizeof(u_node));
    if (new_node == NULL)
    {
        ERRLOG("节点创建失败\n");
        return;
    }
    new_node->userm = (user_t *)malloc(sizeof(user_t));
    if (new_node->userm == NULL)
    {
        ERRLOG("用户信息创建失败\n");
        free(new_node);
        return;
    }
    // memcpy(new_node->userm, &userdt, sizeof(user_t));
    strncpy(new_node->userm->name, userdt.name, 15);
    new_node->userm->name[15] = '\0';
    strncpy(new_node->userm->passwd, userdt.passwd, 63);
    new_node->userm->passwd[63] = '\0';
    new_node->userm->IDcard = userdt.IDcard;
    new_node->userm->value = userdt.value;

    for (int i = 0; i < 10; i++)
    {
        new_node->userm->ticket[i] = NULL;
    }
    new_node->next = NULL;

    if (pos == 1 || user_list->next == NULL)
    {
        new_node->next = user_list->next;
        user_list->next = new_node;
    }
    else
    {
        u_node *ptr = user_list->next;
        int i;
        for (i = 1; i < pos - 1 && ptr != NULL; i++)
        {
            ptr = ptr->next;
        }

        if (ptr == NULL)
        {
            // 如果 pos 超过了链表长度，插入到链表尾部
            ptr = user_list->next;
            while (ptr->next != NULL)
            {
                ptr = ptr->next;
            }
        }

        new_node->next = ptr->next;
        ptr->next = new_node;
    }
}
// 用户表的删除
void delete_user(u_list *user_list, int pos)
{
    if (user_list == NULL)
    {
        ERRLOG("头节点为空\n");
        return;
    }
    if (user_list->next == NULL)
    {
        ERRLOG("链表为空\n");
        return;
    }

    u_node *ptr = user_list->next;
    u_node *prev = NULL; // 初始化为 NULL
    int i;

    for (i = 1; i < pos && ptr != NULL; i++)
    {
        prev = ptr;
        ptr = ptr->next;
    }

    if (ptr == NULL)
    {
        ERRLOG("位置超出链表范围\n");
        return;
    }

    if (prev == NULL)
    {
        // 删除的是第一个节点
        user_list->next = ptr->next;
    }
    else
    {
        // 删除的是中间或最后一个节点
        prev->next = ptr->next;
    }

    if (ptr->userm != NULL)
    {
        free(ptr->userm);
    }
    free(ptr);
}
// 遍历用户链表
void print_user_list(u_list *user_list)
{
    if (user_list == NULL)
    {
        ERRLOG("链表为空\n");
        return;
    }
    u_node *ptr = user_list->next;
    while (ptr != NULL)
    {
        printf("用户身份证信息:%d 用户名:%s  密码:%s  票价:%d  \n",
               ptr->userm->IDcard, ptr->userm->name,
               ptr->userm->passwd, ptr->userm->value);
        ptr = ptr->next;
    }
}

// 用户等级的查找
u_node * search_user_name(u_list *user_list)
{
    user_t user;
    if (user_list == NULL)
    {
        ERRLOG("链表为空\n");
        return NULL;
    }
    printf("请输入要修改的用户名:");
    char name[20];
    scanf("%s", name);
    u_node *ptr = user_list->next;
    while (ptr != NULL)
    {
        if (ptr->userm->name == user.name)
        {
            printf("查找成功!!!用户名:%s  密码:%s  票价:%d  \n",
                   ptr->userm->name, ptr->userm->passwd, ptr->userm->value);
            return ptr;
        }
        ptr = ptr->next;
    }
    printf("查找失败!!!\n");
    return NULL;
}
// 用户表的修改
void modify_user_name(u_list *user_list)
{
    if (user_list == NULL)
    {
        ERRLOG("链表为空\n");
        return;
    }
    u_node *ptr = search_user_name(user_list);
    if (ptr != NULL)
    {
        printf("请输入新的用户名：");
        scanf("%s", ptr->userm->name);
    }
}
void modify_user_passwd(u_list *user_list)
{
    if (user_list == NULL)
    {
        ERRLOG("链表为空\n");
        return;
    }
    u_node *ptr = search_user_name(user_list);
    if (ptr != NULL)
    {
        printf("请输入新的密码：");
        scanf("%s", ptr->userm->passwd);
    }
}
void modify_user_value(u_list *user_list) //************************************
{
    if (user_list == NULL)
    {
        ERRLOG("链表为空\n");
        return;
    }
    u_node *ptr = search_user_name(user_list);
    if (ptr != NULL)
    {
        printf("请输入新的票价：");
        scanf("%d", &ptr->userm->value);
    }
}
// 用户表的销毁
void free_user_list(u_list **user_list)
{
    if (user_list == NULL || *user_list == NULL)
    {
        ERRLOG("头节点为空\n");
        return;
    }

    u_node *ptr1 = (*user_list)->next;
    u_node *ptr2 = NULL; // 保存下一个节点

    while (ptr1 != NULL)
    {
        ptr2 = ptr1->next;
        if (ptr1->userm != NULL)
        {
            free(ptr1->userm);
        }
        free(ptr1);
        ptr1 = ptr2;
    }

    free(*user_list);
    *user_list = NULL;
}